package com.song.springclouddemoshardingjdbc.service;

import com.song.springclouddemoshardingjdbc.entity.Order;
import com.song.springclouddemoshardingjdbc.entity.User;
import com.song.springclouddemoshardingjdbc.mapper.OrderMapper;
import com.song.springclouddemoshardingjdbc.mapper.UserMapper;
import io.shardingsphere.transaction.annotation.ShardingTransactionType;
import io.shardingsphere.transaction.api.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserOrderService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;


    @ShardingTransactionType(TransactionType.XA)   // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    @Transactional(rollbackFor = Exception.class)
    public void saveUserOrder(User user, Order order) {

        orderMapper.addOrder(order);
        userMapper.addUser(user);
        //int a = 1/0; //测试回滚，统一提交的话，将这行注释掉就行

    }
}
